package config.Strategy;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import config.Config;

import java.util.Objects;

/**
 * @Description config
 * @Author zhengzetao
 * @Date 2022/9/18 18:32
 */
public class EntityConfig extends Config {

    final private static String GROUP = "StrategyConfig.Entity";

    //superClass(String)	设置父类	com.baomidou.global.BaseEntity
    private String superClass;
    //disableSerialVersionUID	禁用生成 serialVersionUID	默认值:true
    private boolean disableSerialVersionUID;
    //enableChainModel	开启链式模型	默认值:false
    private boolean enableChainModel;
    //enableLombok	开启 lombok 模型	默认值:false
    private boolean enableLombok;
    //enableRemoveIsPrefix	开启 Boolean 类型字段移除 is 前缀	默认值:false
    private boolean enableRemoveIsPrefix;
    //logicDeleteColumnName(String)	逻辑删除字段名(数据库)
    private String logicDeleteColumnName;
    //logicDeletePropertyName(String)	逻辑删除属性名(实体)
    private String logicDeletePropertyName;
    //enableActiveRecord	开启 ActiveRecord 模型	默认值:false
    private boolean enableActiveRecord;
    //addIgnoreColumns(String...)	添加忽略字段
    private String[] ignoreColumns;
    //idType(IdType)	全局主键类型
    private IdType idType;
    //formatFileName(String)	格式化文件名称 %sEntity
    private String formatFileName;

    private EntityConfig(){
        super(GROUP);
        this.superClass = getStr("superClass");
        this.disableSerialVersionUID = getBool("disableSerialVersionUID",false);
        this.enableChainModel = getBool("enableChainModel",false);
        this.enableLombok = getBool("enableLombok",false);
        this.enableRemoveIsPrefix = getBool("enableRemoveIsPrefix",false);
        this.logicDeleteColumnName = getStr("logicDeleteColumnName");
        this.logicDeletePropertyName = getStr("logicDeletePropertyName");
        this.enableActiveRecord = getBool("enableActiveRecord",false);
        this.ignoreColumns = getStrings("addIgnoreColumns");
        switch (getStr("idType", "")) {
            case "ASSIGN_ID" -> this.idType = IdType.ASSIGN_ID;
            case "ASSIGN_UUID" -> this.idType = IdType.ASSIGN_UUID;
            case "AUTO" -> this.idType = IdType.AUTO;
            case "NONE" -> this.idType = IdType.NONE;
            case "INPUT" -> this.idType = IdType.INPUT;
        }
        this.formatFileName = getStr("formatFileName");
    }

    public static void setting(Entity.Builder builder){
        EntityConfig entityConfig = new EntityConfig();
        builder.superClass(entityConfig.superClass).logicDeleteColumnName(entityConfig.logicDeleteColumnName)
                .logicDeletePropertyName(entityConfig.logicDeletePropertyName);
        if(entityConfig.enableActiveRecord){
            builder.enableActiveRecord();
        }
        if(entityConfig.disableSerialVersionUID) {
            builder.disableSerialVersionUID();
        }
        if(entityConfig.enableChainModel){
            builder.enableChainModel();
        }
        if(entityConfig.enableLombok){
            builder.enableLombok();
        }
        if(entityConfig.enableRemoveIsPrefix){
            builder.enableRemoveIsPrefix();
        }
        if(Objects.nonNull(entityConfig.ignoreColumns)){
            builder.addIgnoreColumns(entityConfig.ignoreColumns);
        }
        if(Objects.nonNull(entityConfig.idType)){
            builder.idType(entityConfig.idType);
        }
        if(Objects.nonNull(entityConfig.formatFileName)){
            builder.formatFileName(entityConfig.formatFileName);
        }
    }
}
